package com.ruoyi.web.controller.system;

import java.util.List;


import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.domain.SysNoticeExt;
import com.ruoyi.system.service.ISysNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysDictItem;
import com.ruoyi.system.service.ISysDictItemService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 字典子项Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/dictItem")
public class SysDictItemController extends BaseController
{
    @Autowired
    private ISysDictItemService sysDictItemService;

    @Autowired
    private ISysNoticeService noticeService;

    /**
     * 查询字典子项列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysDictItem sysDictItem)
    {
        startPage();
        List<SysDictItem> list = sysDictItemService.selectSysDictItemList(sysDictItem);
        return getDataTable(list);
    }

    /**
     * 根据字典类型查询字典子项
     */
    @GetMapping("/type/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType)
    {
        return AjaxResult.success(sysDictItemService.selectSysDictItemByDictType(dictType));
    }

    /**
     * 导出字典子项列表
     */
    @PreAuthorize("@ss.hasPermi('system:dictItem:export')")
    @Log(title = "字典子项", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysDictItem sysDictItem)
    {
        List<SysDictItem> list = sysDictItemService.selectSysDictItemList(sysDictItem);
        ExcelUtil<SysDictItem> util = new ExcelUtil<SysDictItem>(SysDictItem.class);
        return util.exportExcel(list, "字典子项数据");
    }

    /**
     * 获取字典子项详细信息
     */
    @GetMapping(value = "/{itemId}")
    public AjaxResult getInfo(@PathVariable("itemId") Long itemId)
    {
        return AjaxResult.success(sysDictItemService.selectSysDictItemById(itemId));
    }

    /**
     * 新增字典子项
     */
    @Log(title = "字典子项", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDictItem sysDictItem)
    {
        sysDictItem.setCreateBy(getUsername());
        return toAjax(sysDictItemService.insertSysDictItem(sysDictItem));
    }

    /**
     * 修改字典子项
     */
    @Log(title = "字典子项", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDictItem sysDictItem)
    {
        sysDictItem.setUpdateBy(getUsername());
        return toAjax(sysDictItemService.updateSysDictItem(sysDictItem));
    }

    /**
     * 删除字典子项
     */
    @PreAuthorize("@ss.hasPermi('system:dictItem:remove')")
    @Log(title = "字典子项", businessType = BusinessType.DELETE)
	@DeleteMapping("/{itemIds}")
    public AjaxResult remove(@PathVariable Long[] itemIds)
    {
        Long itemId = itemIds[0];
        SysNotice notice = new SysNotice();
        notice.setNoticeSubType(itemId);
        List<SysNoticeExt> list = noticeService.selectNoticeListWithSubType(notice);
        // 检查是否有关联的文章内容
        if (list == null || list.isEmpty()) {
            // 没有关联数据，执行删除操作
            return toAjax(sysDictItemService.deleteSysDictItemByIds(itemIds));
        } else {
            // 有关联数据，返回错误提示
            return error("该子类下还有文章，请先去删除文章");
        }
    }
}
    